package com.bookmanger.server.controller;

import com.bookmanger.common.result.Result;
import com.bookmanger.pojo.DTO.*;
import com.bookmanger.pojo.Entity.*;
import com.bookmanger.pojo.VO.BorrowVO;
import com.bookmanger.pojo.VO.PrebookVO;
import com.bookmanger.server.service.ReaderService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    private ReaderService readerService;


    @PostMapping("/getself")
    public Result getSelf(@RequestParam int readerId) //读者-获取个人信息
    {
        Reader reader=readerService.getself(readerId);
        if(reader!=null)
            return Result.success(reader);
        else
            return Result.error("无法获取个人信息");
    }
    @PostMapping("/updateself")
    public Result updateself(@RequestBody Reader reader) //读者-修改个人信息
    {
      Reader readers= readerService.updateself(reader);
       if(readers!=null)
           return Result.success(readers);
       else
           return Result.error("修改个人信息失败");
    }

   @PostMapping("/getborrows")
   public Result getborrow(@RequestParam int readerId)//读者--查询自己借阅还未归还同时没有挂失的书-我的书架
   {
        List<BorrowVO> borrows=readerService.getborrows(readerId);
        return Result.success(borrows);
   }

   @PostMapping("/getlossborrows")
   public Result getLossBorrows(@RequestParam int readerId)//读者--查询自己借阅未归还且挂失的书--我的挂失
   {
       List<BorrowVO> borrows=readerService.getLossBorrows(readerId);
       return Result.success(borrows);
   }

   @PostMapping("/getreturnedunapproved")
   public Result getReturnedUnapproved(@RequestParam int readerId)//读者--查询已归还但未审批的书--我的归还
   {
       List<BorrowVO> borrows=readerService.getReturnedUnapprovedBorrows(readerId);
       return Result.success(borrows);
   }

   @PostMapping("/renew")
    public Result renew(@RequestBody RenewDTO renew)//读者续借图书
   {
       readerService.renew(renew);
       return Result.success("续借成功");

   }
 @PostMapping("/lossbook")
    public Result lossbook(@RequestParam int readerId,@RequestParam int bookId)//读者-挂失图书
 {
     readerService.lossbook(readerId,bookId);

     return Result.success("挂失成功");

 }

    @PostMapping("/cancellossbook")
    public Result cancellossbook(@RequestParam int readerId,@RequestParam int bookId)//读者-取消挂失图书
    {
       readerService.cancellossbook(readerId,bookId);
       return Result.success("取消挂失成功");
    }


    @PostMapping("/getbooks")
    public Result getbooks()//获取所有图书信息
    {
     List<Book> books =readerService.getbooks();
     return Result.success(books);
    }

    @PostMapping("/changepassword")
    public Result changepassword(@RequestBody PasswordDTO passwordDTO)//读者修改个人密码
    {
        readerService.changepassword(passwordDTO);
        return Result.success("密码修改成功");
    }

    @PostMapping("/returnbook")
    public Result returnbook(@RequestParam int readerId,@RequestParam int bookId)//读者归还图书
    {
        readerService.returnbook(readerId,bookId);
        return Result.success();
    }

	@PostMapping("/findrecords")
	public Result findrecords(@RequestBody FindRecordDTO findRecordDTO)//读者-分页查询借阅记录
	{
		return Result.success(readerService.findBorrowRecords(findRecordDTO));
	}

    @PostMapping("/prebook")
    public Result prebook(@RequestBody PrebookDTO prebookDTO)//预约图书
    {
        readerService.prebook(prebookDTO);
        return Result.success("预约成功");
    }

    @PostMapping("/getprebooks")
    public Result getPrebooks(@RequestParam int readerId)//读者--查询所有有效预约记录//我的预约
    {
        List<PrebookVO> list = readerService.getPrebooks(readerId);
        return Result.success(list);
    }

    @PostMapping("/cancelprebook")
    public Result cancelPrebook(@RequestParam int id)//读者--取消预约
    {
        readerService.cancelPrebook(id);
        return Result.success("取消预约成功");
    }

    @PostMapping("/getallprebooks")
    public Result getallPrebooks(@RequestBody FindPrebookDTO findPrebookDTO)//读者--查询所有有效预约记录/预约记录
    {

        return Result.success( readerService.getallPrebooks(findPrebookDTO));
    }


 }
